/**
 * 资源笔记查看
 */
//显示笔记列表
var currNoteids='';//已有笔记
var noteList=new Array();;
var plusStorageManagers=require('script/plusStorageManagers.js');

//打开笔记记录页面
mui('header').on('tap','#opennoteview',openNoteview);
mui('#item2mobile').on('tap','.note',openNoteContent);

function initCoursenotesDiv(){
	document.getElementById('coursenotes-div').innerHTML='<div class="nonotetip">没有选择资源或该资源还没有笔记</div>';
}
//加载笔记
function loadNotes(){
	currNoteids='';//已有资源
	noteList=new Array();
	initCoursenotesDiv();
	if(hasNet()){
		loadNoteAjax();
	}else{
		//loadNoteLocal();
	}
}
//加载本地笔记
function loadNoteLocal(){
	var courseid=document.getElementById('fCourseid').value;
	//显示本地缓存资源
	var noteList=plusStorageManagers.getNotesSessionByCourseid(courseid);
	if(noteList!=null&&noteList.length>0)clearNotesDiv(noteList);
	buildNoteDivs(noteList,false);
}
//ajax加载笔记
function loadNoteAjax(){
	var courseid=document.getElementById('fCourseid').value;
	var userid=localStorageManagers.getUseridSession();
	var catalogid=document.getElementById('fCatalogid').value;
	var resid=document.getElementById('fResid').value;
	mui.ajax(serverAjax+'mobile/note/getNotesByResidAndStudentid.do', {
		data: {
			userid: userid,
			currNoteids:currNoteids,
			courseid:courseid,
			catalogid:catalogid,
			resid:resid
		},
		dataType: 'json', //服务器返回json格式数据
		type: 'post', //HTTP请求类型
		timeout: 15000, //超时时间设置为10秒；
		success: function(data) {
			if(data!=null&&data.length>0)clearNotesDiv(noteList);
			if(data==null||data.length==0)return;
			buildNoteDivs(data,true);
			//mui.toast("data"+data.length);
			plusStorageManagers.setNotesSessionByCourseid(courseid,noteList);
		},
		error: function(xhr, type, errorThrown) {
			console.log(xhr);
			ajaxErrorTip();
		}
	});
}
//构建笔记列表,ifAjax:是否是从ajax加载
function buildNoteDivs(noteListTemp,ifAjax){
	if(noteListTemp==null)return;
		
	for(var i=0; i < noteListTemp.length; i++) {
				var note = noteListTemp[i];
				if(!ifAjax)currNoteids+=note.noteId+",";
				if(ifAjax)noteList.push(note);//ajax加载的内容需要添加到缓存
				
		        buildNoteDiv(note);
			}
	if(currNoteids.length>0)currNoteids=currNoteids.substring(0,currNoteids.length-1);
}
//构建笔记div
function buildNoteDiv(note){
	var catalogid=note.catalogId;
	//课题div是否在
	var coursenote_div=document.getElementById("coursenote-div"+catalogid);
	var zaned='';
	if(note.enlighten==1)zaned='zaned';
	//var restitle=res.title;
	//if(restitle!=null&&restitle.length>10)restitle=restitle.substring(0,13)+'...';
	//var status=getInitBtnStatus(res.catalogId,res.resId);
    //var btnStatusText=getBtnViewtext(status);
    var catClass=getCatClass(note.resCatName);
    var restitle=note.resName;
    if (restitle != null && restitle.length > 8) restitle = restitle.substring(0, 8) + '...';
	if(coursenote_div==null){
		var coursenotesdiv=document.getElementById('coursenotes-div');
		var coursenotediv='<div class="coursenote-div" id="coursenote-div'+catalogid+'">'
									+'<div class="catalog-name"><p>'+note.catalogName+'</p></div>'
									+'<div class="notes" id="course-note'+catalogid+'">'
										+'<div class="note"  noteid="'+note.noteId+'" rescatname="'+note.resCatName+'" resname="'+note.resName+'" notecontent="'+note.noteContent+'">'
											+'<div class="c1"><span class="restype-div '+catClass+'">'+note.resCatName+'</span><span class="resname-div">'+restitle+'</span><span class="zan '+zaned+'"></span><span class="date">'+note.noteDate+'</span></div> '
											+'<div class="c2">'+note.noteContent+'</div>'
										+'</div>'
									+'</div>'
								+'</div>';
								
		coursenotesdiv.innerHTML=coursenotesdiv.innerHTML+coursenotediv;
	}else{
		var coursenotediv=document.getElementById("course-note"+catalogid);
		var notediv='<div class="note" noteid="'+note.noteId+'" rescatname="'+note.resCatName+'" resname="'+note.resName+'" notecontent="'+note.noteContent+'">'
						+'<div class="c1"><span class="restype-div '+catClass+'">'+note.resCatName+'</span><span class="resname-div">'+restitle+'</span><span class="zan '+zaned+'"></span><span class="date">'+note.noteDate+'</span></div> '
						+'<div class="c2">'+note.noteContent+'</div>'
					+'</div>';
	   coursenotediv.innerHTML=coursenotediv.innerHTML+notediv;	
	}
}
//
function clearNotesDiv(){
	//清空原本内容
	document.getElementById('coursenotes-div').innerHTML='';
}

//打开笔记记录页面
function openNoteview(){
	pauseVideo();
	var courseid=document.getElementById('fCourseid').value;
	var catalogid=document.getElementById('fCatalogid').value;
	var resid=document.getElementById('fResid').value;
	var restitle=document.getElementById('fResname').value;
	var rescatname=document.getElementById('fRescatname').value;
	if(resid==0||resid==''){
		mui.toast('请选择资源');
		return;
	}
	mui.openWindow({
		url:'addNote.html',
		id:'synccourse/addNote',
		styles:getStyles(),
		extras:{'courseid':courseid,'catalogid':catalogid,'resid':resid,'restitle':restitle,'rescatname':rescatname}
	});
};
//笔记详情
function openNoteContent(){
	pauseVideo();
	var node=this;
   var noteid = node.getAttribute('noteid');
   var rescatname = node.getAttribute('rescatname');
   var resname = node.getAttribute('resname');
   var notecontent= node.getAttribute('notecontent');
   mui.openWindow({
		url: 'noteContent.html',
		id: 'synccourse/noteContent',
		styles:getStyles(),
		extras: {
			noteid: noteid,
			rescatname:rescatname,
			resname:resname,
			notecontent:notecontent
		}
	});
}

